

/******************** 定义类 ***************************************************************/
/*
    管理员角色类
 */
function Investment() {
    this.id = null; // 唯一标识
    this.title = ""; // 标题
    this.member = null;
    this.create_time = ""; // 创建时间
    this.formated_create_time = ""; // 格式化的创建时间
    this.capital_type = ""; // 资金类型
    this.area = []; // 地区
    this.investment_industry = []; // 投资行业
    this.investment_area = []; // 投资地区
    this.amount_min = 0; // 最小投资金额
    this.amount_max = 0; // 最大投资金额
    this.term_min = 0; // 最小投资年限
    this.term_max = 0; // 最大投资年限
    this.investment_type = ""; // 投资类型
    this.needed_appendix = []; // 需要提供的附件
    this.requirement = ""; // 投资要求
    this.remark = ""; // 备注
    this.thumbnail = { dataUrl: "" }; // 缩略图
    this.appendix_ids = [];
    this.appendix = []; // 附件
    this.investment_stage = ""; // 投资阶段
    this.min_return = 0; // 最低回报
    this.failed_reason = ""; // 失败原因
    this.status = 0;
}

// 将对象序列化为一个json对象，可以用于http请求或对象复制
Investment.prototype.serialize = function() {
    return {
        id: this.id,
        title: this.title,
        capital_type: this.capital_type,
        area: this.area.join(","),
        investment_industry: this.investment_industry.join(","),
        investment_area: this.investment_area.join(","),
        amount_min: this.amount_min,
        amount_max: this.amount_max,
        term_min: this.term_min,
        term_max: this.term_max,
        investment_type: this.investment_type,
        needed_appendix: this.needed_appendix.join(","),
        requirement: this.requirement,
        remark: this.remark,
        thumbnail: this.thumbnail.dataUrl,
        appendix_ids: this.appendix.map(function(elem){return elem.id}).join(","),
        investment_stage: this.investment_stage,
        min_return: this.min_return,
        failed_reason: this.failed_reason,
        status: this.status
    }
}

// 用json数据为对象属性赋值
Investment.prototype.parse = function(data) {
    this.id = data.id;
    this.title = data.title;
    this.member = data.member;
    this.create_time = data.create_time;
    this.formated_create_time = new Date(parseInt(data.create_time)).toLocaleDateString();
    this.capital_type = data.capital_type;
    this.area = data.area.split(",");
    this.investment_industry = data.investment_industry.split(",");
    this.investment_area = data.investment_area.split(",");
    this.amount_min = data.amount_min;
    this.amount_max = data.amount_max;
    this.term_min = data.term_min;
    this.term_max = data.term_max;
    this.investment_type = data.investment_type;
    this.needed_appendix = data.needed_appendix.split(",");
    this.requirement = data.requirement;
    this.remark = data.remark;
    this.thumbnail.dataUrl = data.thumbnail;
    this.appendix_ids = data.appendix_ids.split(",");
    this.appendix = data.appendix;
    this.investment_stage = data.investment_stage;
    this.min_return = data.min_return;
    this.failed_reason = data.failed_reason;
    this.status = data.status;
}

// 暂时将属性存起来，做一次备份
Investment.prototype.storageValue = function() {
    this.storage = this.serialize();
    this.storage.appendix = this.appendix;
    this.storage.member = this.member;
    this.storage.create_time = this.create_time;
}

// 用备份的数据覆盖现有数据
Investment.prototype.applyStorageValue = function() {
    if(this.storage) {
        this.parse(this.storage);
    }
}

// ******************** 类静态属性与方法 **********************************************
// 解析json数据，返回一个对象
Investment.parse = function(data) {
    var o = new Investment();
    o.parse(data);
    return o;
}

// 解析json数组，返回对象数组
Investment.parseList = function(list) {
    var len = list.length;
    var arr = new Array(len);
    for(var i = 0; i < len; i++) {
        arr[i] = Investment.parse(list[i]);
    }
    return arr;
}

// ******************** 数据库存储方法 **********************************************
// Investment.prototype.save = function(callback) {
//     var that = this;
//     var action = this.mode == "add" ? "article_add" : "article_modify";
//     api.post(action, this.serialize(), function(res) {
//         that.id = res.id;
//         if(callback) callback();
//     });
// }



// ******************** 导出 **********************************************
export default Investment